package com.chenss.dragonser.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chenss.dragonser.R;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.listener.OnMyDialogTwoListener;
import com.chenss.dragonser.model.OrderRetrunListModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.response.OrderListResponse;
import com.chenss.dragonser.response.OrderReturnDetailResponse;
import com.chenss.dragonser.response.OrderReturnListResponse;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.ToastUtil;
import com.chenss.dragonser.view.XDialogTwo;

import org.json.JSONException;

import java.util.List;
import java.util.logging.Logger;

/**
 * PageName: 退货订单详情
 * Create by chenss at 2018/4/20 2:54
 */
public class OrderReturnDetailActivity extends BaseActivity implements RequestCallbcak,OnMyDialogTwoListener {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    int status = 0;
    TextView tv_bottom, tv_bottom_btn;//底部
    TextView tv_username, tv_mobile, tv_addr;
    TextView tv_goosName, tv_goodsPrice, tv_count;
    TextView tv_reason, tv_return_des;
    TextView tv_all, tv_all_price;
    TextView tv_sale_num;
    TextView tv_mark;
    TextView tv_point1, tv_point2, tv_point3;
    TextView tv_point_tip1, tv_point_tip2, tv_point_tip3;
    ImageView img_point1, img_point2, img_point3;
    ImageView img_goods;

    LinearLayout ll_btm;
    ImageView img1,img2,img3,img4;


    String returnGoodsId = "";
    String return_userid = "0";
    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装
    XDialogTwo dialogTwo;
    @Override
    public void initParms(Bundle parms) {
        status = getIntent().getExtras().getInt("status", 0);
        returnGoodsId = getIntent().getExtras().getString("goods_return_id");
        return_userid = getIntent().getExtras().getString("userid");
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_order_return_detail;
    }

    @Override
    public void initView(View view) {
        dialogTwo=new XDialogTwo(this);
        ll_left = findView(R.id.ll_left);//左边按钮
        tv_page_title = findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("订单详情");

        tv_bottom = findView(R.id.tv_bottom);
        tv_bottom_btn = findView(R.id.tv_bottom_btn);
        tv_username = findView(R.id.tv_username);
        tv_mobile = findView(R.id.tv_mobile);
        tv_addr = findView(R.id.tv_addr);

        tv_mark = findView(R.id.tv_mark);
        tv_reason = findView(R.id.tv_reason);
        tv_return_des = findView(R.id.tv_return_des);
        tv_all = findView(R.id.tv_all);
        tv_all_price = findView(R.id.tv_all_price);
        tv_point1 = findView(R.id.tv_point1);
        tv_point2 = findView(R.id.tv_point2);
        tv_point3 = findView(R.id.tv_point3);

        tv_point_tip1 = findView(R.id.tv_point_tip1);
        tv_point_tip2 = findView(R.id.tv_point_tip2);
        tv_point_tip3 = findView(R.id.tv_point_tip3);

        tv_sale_num = findView(R.id.tv_sale_num);
        tv_goosName = findView(R.id.tv_goosName);
        tv_goodsPrice = findView(R.id.tv_goodsPrice);
        tv_count = findView(R.id.tv_count);


        img_goods = findView(R.id.img_goods);
        img_point1 = findView(R.id.img_point1);
        img_point2 = findView(R.id.img_point2);
        img_point3 = findView(R.id.img_point3);

        ll_btm = findView(R.id.ll_btm);
        ll_btm.setVisibility(View.GONE);


        img1 = findView(R.id.img1);
        img2 = findView(R.id.img2);
        img3 = findView(R.id.img3);
        img4 = findView(R.id.img4);

        tv_bottom.setText("退款金额：");
        tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(this, R.color.textLightGray));

    }

    @Override
    public void doBusiness(Context context) {
        networkRequest = new NetworkRequest(this, this);
        getDetailsInfoDatas();
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ll_left:
                finish();
                break;
        }
    }
    @Override
    public void onSubmitClick(int tag) {
        switch (tag) {
            case 1:
                agreeReturnOrder();
                break;
        }
    }

    @Override
    public void onCancleClick(int tag) {

    }
    /**
     * 获取信息
     */
    private void getDetailsInfoDatas() {
        /* 修改这里 - 下方为接口所要传递的参数 */
//                'userid' => '', // 用户id 用户端传 没有否则传0
//                'storehouse_id' => '', // 门店id 商户端传 没有否则传0
//                'goods_return_id' => '', // 退换货单id
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Order, Constant.classes.Order_an_ReturnGoodsInfo);
        parmMap.put("storehouse_id", AppInfo.getUser(this).getStorehouse_id());//服务站用户id

        if (TextUtils.isEmpty(return_userid)) {
            parmMap.put("userid", "0");
        } else {
            parmMap.put("userid", return_userid);
        }
        parmMap.put("goods_return_id", returnGoodsId);
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, true, Constant.classes.Order_an_ReturnGoodsInfo);
    }

    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            OrderReturnDetailResponse returnDetailResponse = GsonUtil.GsonToBean(result, OrderReturnDetailResponse.class);
            if (null != returnDetailResponse) {

                if (null != returnDetailResponse.getData().getReturn_goods_info()) {
                    tv_return_des.setText("退货描述：" + returnDetailResponse.getData().getReturn_goods_info().getReturn_message());
                    tv_reason.setText("退货原因：" + returnDetailResponse.getData().getReturn_goods_info().getReturn_reason());
                    tv_username.setText(returnDetailResponse.getData().getReturn_goods_info().getReturn_receiver_name());
                    tv_mobile.setText(returnDetailResponse.getData().getReturn_goods_info().getReturn_receiver_phone());
                    tv_addr.setText(returnDetailResponse.getData().getReturn_goods_info().getReturn_receiver_address());
                    tv_all_price.setText("￥" +returnDetailResponse.getData().getReturn_goods_info().getReturn_goods_amount()+"");
                    tv_goosName.setText(returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getGoods_name());
                    tv_goodsPrice.setText("￥" +returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getSale_price());
                    double all=Double.parseDouble(returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getSale_price())*Integer.parseInt(returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getSale_num());
                    tv_all.setText("￥" +all);
                    tv_count.setText("x"+returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getSale_num());
                    tv_sale_num.setText("总数：" + returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getSale_num());
                    tv_mark.setText("备注：无");
                    final String type=returnDetailResponse.getData().getReturn_goods_info().getReturn_type();
//                   退货状态【1:待审核】【2:待退货】【3:待换货】【4:已完成】【5审核未通过】
                    switch (Integer.parseInt(returnDetailResponse.getData().getReturn_goods_info().getReturn_status())) {
                        case 1:
                            if(returnDetailResponse.getData().getReturn_goods_info().getReturn_type().equals("1")){
                                tv_bottom_btn.setText("同意退货");
                            }else{
                                tv_bottom_btn.setText("同意换货");
                            }
                            tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(OrderReturnDetailActivity.this, R.color.appColor));
                            tv_bottom_btn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if(type.equals("1")){
                                        dialogTwo.showTwoDialog(OrderReturnDetailActivity.this, "同意退货？", "确定", "取消", "提示", 1);
                                    }else{
                                        dialogTwo.showTwoDialog(OrderReturnDetailActivity.this, "同意换货？", "确定", "取消", "提示", 1);
                                    }
                                }
                            });
                            break;
                        case 2:
                            tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(OrderReturnDetailActivity.this, R.color.textLightGray));
                            tv_bottom_btn.setText("等待平台同意退货");
                            break;
                        case 3:
                            tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(OrderReturnDetailActivity.this, R.color.textLightGray));
                            tv_bottom_btn.setText("等待平台同意换货");
                            break;
                        case 4:
                            tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(OrderReturnDetailActivity.this, R.color.textLightGray));
                            tv_bottom_btn.setText("已退款");
                            break;
                        case 5:
                            tv_bottom_btn.setBackgroundColor(ContextCompat.getColor(OrderReturnDetailActivity.this, R.color.textLightGray));
                            tv_bottom_btn.setText("退款申请未通过");
                            break;

                    }
                    if (returnDetailResponse.getData().getReturn_goods_info().getReturn_cashcoupon_deductible() == 0) {
                        img_point1.setImageResource(R.mipmap.icon_point_2);
                        tv_point1.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                        tv_point_tip1.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                    } else {
                        img_point1.setImageResource(R.mipmap.icon_point_1);
                        tv_point1.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                        tv_point_tip1.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                    }
                    if (returnDetailResponse.getData().getReturn_goods_info().getReturn_coupon_deductible() == 0) {
                        img_point2.setImageResource(R.mipmap.icon_point_2);
                        tv_point2.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                        tv_point_tip2.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                    } else {
                        img_point2.setImageResource(R.mipmap.icon_point_1);
                        tv_point2.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                        tv_point_tip2.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                    }
                    String useBalance = returnDetailResponse.getData().getReturn_goods_info().getUse_balance();
                    if (!TextUtils.isEmpty(useBalance) && Double.parseDouble(useBalance) > 0) {
                        img_point1.setImageResource(R.mipmap.icon_point_1);
                        tv_point1.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                        tv_point_tip1.setTextColor(ContextCompat.getColor(this, R.color.appColor));
                    } else {
                        img_point1.setImageResource(R.mipmap.icon_point_2);
                        tv_point1.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                        tv_point_tip1.setTextColor(ContextCompat.getColor(this, R.color.textLightGray));
                    }
                    Glide.with(this).load(returnDetailResponse.getData().getReturn_goods_info().getOrder_goods().get(0).getGoods_image()).into(img_goods);
                    tv_point1.setText("￥" + useBalance);
                    tv_point2.setText("￥" + returnDetailResponse.getData().getReturn_goods_info().getReturn_coupon_deductible());
                    tv_point3.setText("￥" + returnDetailResponse.getData().getReturn_goods_info().getReturn_cashcoupon_deductible());
                    List<String> urlList=returnDetailResponse.getData().getReturn_goods_info().getReturn_goods_img();
                    if(null!=urlList&&urlList.size()>0){
                        switch (urlList.size()) {

                            case 1:
                                ll_btm.setVisibility(View.VISIBLE);
                                setAllGone(img2, img3, img4);
                                setAllVisib(img1);
                                Glide.with(this).load(urlList.get(0)).into(img1);
                                break;
                            case 2:
                                ll_btm.setVisibility(View.VISIBLE);
                                setAllGone(img3, img4);
                                setAllVisib(img1, img2);
                                Glide.with(this).load(urlList.get(0)).into(img1);
                                Glide.with(this).load(urlList.get(1)).into(img2);
                                break;
                            case 3:
                                ll_btm.setVisibility(View.VISIBLE);
                                setAllGone(img4);
                                setAllVisib(img1, img2, img3);
                                Glide.with(this).load(urlList.get(0)).into(img1);
                                Glide.with(this).load(urlList.get(1)).into(img2);
                                Glide.with(this).load(urlList.get(2)).into(img3);
                                break;
                            case 4:
                                ll_btm.setVisibility(View.VISIBLE);
                                setAllVisib(img1, img2, img3, img4);
                                Glide.with(this).load(urlList.get(0)).into(img1);
                                Glide.with(this).load(urlList.get(1)).into(img2);
                                Glide.with(this).load(urlList.get(2)).into(img3);
                                Glide.with(this).load(urlList.get(3)).into(img4);
                                break;

                        }
                        }

                    }

            }


        }else   if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_2) {
            getDetailsInfoDatas();
        }
    }

        private void setAllGone(ImageView... imgs){
            for (ImageView img: imgs) {
                img.setVisibility(View.GONE);
            }
        }
        private void setAllVisib(ImageView... imgs){
            for (ImageView img: imgs) {
                img.setVisibility(View.VISIBLE);
            }
        }
    /**
     * 同意退换货
     */
    private void agreeReturnOrder() {
        /* 修改这里 - 下方为接口所要传递的参数 */
//        'userid' => '', // 用户id 用户端传 没有否则传0
//                'storehouse_id' => '', // 门店id 商户端传 没有否则传0
//                'goods_return_id' => '', // 退货单id
//                'return_type' => 'completion', //  订单类型 ，确认退换货 refundable  ，交易完成 completion
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Order, Constant.classes.Order_an_UpdateReturnState);
        parmMap.put("storehouse_id", AppInfo.getUser(this).getStorehouse_id());//服务站用户id
        parmMap.put("userid", "0");
        parmMap.put("goods_return_id", returnGoodsId);
        parmMap.put("return_type", "refundable");
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_2, true, Constant.classes.Order_an_UpdateReturnState);
    }
}
